/**
 * 
 */
package testing;

import interfaces.IMeasure;
import interfaces.IRange;

import java.util.ArrayList;

import junit.framework.TestCase;

import massDistribution.ContinueMeasure;
import massDistribution.ContinueRange;
import massDistribution.DiscreteMeasure;
import massDistribution.DiscreteRange;
import massDistribution.MeasuredAttribute;
import massDistribution.Metric;
import massDistribution.MetricType;
import stubs.AttributeValueGenerator;
import stubs.ServiceStub;
import stubs.TrustLevel;
import exceptions.NormalValueGenerationException;

/**
 * @author Elisa Costante
 * 
 */
public class ValueGeneratorTest extends TestCase {

	protected void setUp() throws Exception {
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testgenerateDiscreteValue()
			throws NormalValueGenerationException {

		MeasuredAttribute altezzaAttribute = new MeasuredAttribute("altezza");

		DiscreteRange range = new DiscreteRange();
		range.addElement(new DiscreteMeasure("Molto Basso"));
		range.addElement(new DiscreteMeasure("Basso"));
		range.addElement(new DiscreteMeasure("Medio"));
		range.addElement(new DiscreteMeasure("Alto"));
		range.addElement(new DiscreteMeasure("Molto Alto"));

		Metric altezzaMetric = new Metric("scala", MetricType.DISCRETE);
		ArrayList<IRange> validRanges = new ArrayList<IRange>();
		validRanges.add(range);
		altezzaMetric.setValidRanges(validRanges);
		altezzaMetric.setBestCase(new DiscreteMeasure("Molto Alto"));
		altezzaMetric.setWorstCase(new DiscreteMeasure("Molto Basso"));
		altezzaMetric.setMediumCase(new DiscreteMeasure("Medio"));
		altezzaAttribute.setMetric(altezzaMetric);

		ServiceStub service = new ServiceStub("Service1");
		service.setTrustValue(TrustLevel.LOW);

		System.out.println("****************** LOW **************");

		for (int i = 0; i < 1000; i++) {
			IMeasure measureGaussian = AttributeValueGenerator
					.getGaussianMeasure(service, altezzaAttribute);
			assertTrue(range.containsValue(measureGaussian));
			System.out.println(measureGaussian.toString());
		}

		service.setTrustValue(TrustLevel.MEDIUM);
		System.out.println("****************** Medium **************");

		for (int i = 0; i < 1000; i++) {
			IMeasure measureGaussian = AttributeValueGenerator
					.getGaussianMeasure(service, altezzaAttribute);
			assertTrue(range.containsValue(measureGaussian));
			System.out.println(measureGaussian.toString());
		}

		service.setTrustValue(TrustLevel.HIGH);

		System.out.println("****************** HIGH **************");

		for (int i = 0; i < 1000; i++) {
			IMeasure measureGaussian = AttributeValueGenerator
					.getGaussianMeasure(service, altezzaAttribute);
			assertTrue(range.containsValue(measureGaussian));
			System.out.println(measureGaussian.toString());
		}

	}

	public void testGenerateContonuousValue()
			throws NormalValueGenerationException {

		// ValidRange [0,INF]
		MeasuredAttribute probability = new MeasuredAttribute("Availability");
		Metric probabilityMetric = new Metric("Percentage of availability",
				MetricType.REAL);
		probabilityMetric.setWorstCase(new ContinueMeasure(new Double(0.0)));
		probabilityMetric.setMediumCase(new ContinueMeasure(new Double(0.5)));
		probabilityMetric.setBestCase(new ContinueMeasure(new Double(1.0)));
		ContinueRange continueRange = new ContinueRange(0.0, 1.0, false, false);
		probabilityMetric.addRange(continueRange);
		probability.setMetric(probabilityMetric);

		ServiceStub service = new ServiceStub("Service1");
		service.setTrustValue(TrustLevel.HIGH);

		System.out.println("****************** HIGH **************");

		for (int i = 0; i < 100; i++) {
			IMeasure measureGaussian = AttributeValueGenerator
					.getGaussianMeasure(service, probability);
			assertTrue(continueRange.containsValue(measureGaussian));
			System.out.println(measureGaussian.toString());

		}
		service.setTrustValue(TrustLevel.MEDIUM);
		System.out.println("****************** MEDIUM **************");

		for (int i = 0; i < 100; i++) {
			IMeasure measureGaussian = AttributeValueGenerator
					.getGaussianMeasure(service, probability);
			assertTrue(continueRange.containsValue(measureGaussian));

			System.out.println(measureGaussian.toString());

		}

		service.setTrustValue(TrustLevel.LOW);
		System.out.println("****************** LOW **************");

		for (int i = 0; i < 100; i++) {
			IMeasure measureGaussian = AttributeValueGenerator
					.getGaussianMeasure(service, probability);
			assertTrue(continueRange.containsValue(measureGaussian));

			System.out.println(measureGaussian.toString());

		}

	}

}
